/*****************************************************************************************
This file is a part of Arise library.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

Arise Engine		Sprite.h

					E-mail:		arise_engine@mail.ru

					Origin by:	DK Engine 0.019,
								dev (avon.dn.ua@gmail.com), Agressor (agress0r@mail.ru);
								Haaf's Game Engine 1.5, 
								Copyright (c) 2003-2004 Relish Games
								http://hge.relishgames.com

created:			27.07.2003
last changes:		04.08.2008
*****************************************************************************************/
#pragma once
#include "..\engine\BaseObject.h"
#include "..\gui\Rect.h"

#define SPRT_NUM_VERTEX		4


struct Quad
{
	Quad()	{ blend = 0; }

	Vertex2d			v[SPRT_NUM_VERTEX];
	LPDIRECT3DTEXTURE9	tex;
	int					blend;
};

#define	SPRTHD_NAME			"name"
#define	SPRTHD_TEXTURE		"texture"
#define	SPRTHD_POS			"position"
#define	SPRTHD_HOTSPOT		"hotspot"
#define	SPRTHD_RECT			"rect"
#define	SPRTHD_FRAMES		"frames"
#define	SPRTHD_SPEED		"speed"
#define	SPRTHD_PLAY			"play"
#define	SPRTHD_ANIM			"animation"
#define	SPRTHD_ANIM_FWD		"FORWARD"
#define	SPRTHD_ANIM_REV		"REVERSE"
#define	SPRTHD_ANIM_PING	"PINGPONG"
#define	SPRTHD_ANIM_LOOP	"LOOP"
#define	SPRTHD_COLOR		"color"
#define	SPRTHD_BLEND		"blend"
#define	SPRTHD_COLORADD		"COLORADD"
#define	SPRTHD_COLORMUL		"COLORMUL"
#define	SPRTHD_ALPHAADD		"ALPHAADD"

struct SpriteHeader
{
	SpriteHeader()
	{
		position[0]	= position[1]	= 0;
		hotspot[0]	= hotspot[1]	= 0;
		frames		= 0;
		speed		= 0;
		animation	= 0;
		play		= 0;
		color		= 0;
		blend		= 0;
	}

	std::string	name;
	std::string	texture;
	Rect		rect;
	float		position[2];
	float		hotspot[2];
	UINT		frames;
	float		speed;
	int			animation;
	bool		play;
	DWORD		color;
	int			blend;
};

class Sprite: public BaseObject
{
private:

	UINT		m_pool_id;
	bool		m_is_playing;
	int			m_anim_mode;
	float		m_speed;
	float		m_last_frame_delta;
	int			m_anim_delta;
	int			m_num_frames;
	int			m_current_frame;

public:

	D3DXVECTOR3	pos_top_left;
	void*		render_device;
	Quad		quad;
	float		tx, ty;
	float		width, height;
	float		frame_width, frame_height;
	float		hot_x, hot_y;
	int			align;
	float		tex_width;
	float		tex_height;

	enum ALIGN
	{
		DEFAULT			= 0,
		CENTER			= 1,
		LEFT			= 2,
		RIGHT			= 3,
		TOP				= 4,
		BOTTOM			= 5,
		TOP_LEFT		= 6,
		TOP_RIGHT		= 7,
		BOTTOM_LEFT		= 8,
		BOTTOM_RIGHT	= 9,
	};

	enum ANIM_TYPE
	{
		FWD				= 0,
		REV				= 1,
		PINGPONG		= 2,
		LOOP			= 4,
	};

	Sprite();
	Sprite(LPDIRECT3DTEXTURE9 tex, const SpriteHeader& sprite_header, float scale_x=1.0f, float scale_y=1.0f);
	Sprite(LPDIRECT3DTEXTURE9 tex, float frame_x, float frame_y, float width, float height, float scale_x=1.0f, float scale_y=1.0f);
	virtual ~Sprite();
	
	void		Render();
	void		Render(float x, float y);
	void		RenderEx(float x, float y, float rot, float hscale=1.0f, float vscale=0.0f);

	void		Play();
	void		Update(float delta_time);

	void		Stop() { m_is_playing = false; }
	void		Resume() { m_is_playing = true; }
	void		Resize(float width_factor, float height_factor);
	void		ResizeDimensions(float width_, float height_);
	void		SetClear();
	void		SetTexture(LPDIRECT3DTEXTURE9 tex)	{quad.tex = tex;}
	void		SetColor(DWORD color, int i=-1);
	void		SetBlendMode(int blend) { quad.blend=blend; }
	void		ApplyPosition(float x, float y);
	void		SetPosition(float x, float y)		{pos_top_left.x = x-hot_x; pos_top_left.y = y-hot_y; ApplyPosition(pos_top_left.x, pos_top_left.y);}
	//void		SetPosition(float x, float y, float scale_x, float scale_y)		{pos_top_left.x = (x*scale_x)-hot_x; pos_top_left.y = (y*scale_y)-hot_y; ApplyPosition(pos_top_left.x, pos_top_left.y);}
	void		SetPositionX(float x){pos_top_left.x = x-hot_x;}
	void		SetPositionY(float y){pos_top_left.y = y-hot_y;}
	void		SetDimensions(float width, float height);
	void		SetDevice(void* device)				{render_device = device;}
	void		SetHotSpot(float x, float y)		{hot_x=x; hot_y=y;}
	void		SetHotSpotCenter()					{hot_x=width/2; hot_y=height/2;}
	void		SetPoolId(UINT pool_id)				{m_pool_id = pool_id;}
	void		SetAlign(UINT align);
	void		SetFramePosition(float frame_x, float frame_y);
	void		SetAnimMode(int mode);
	void		SetFrame(int n);
	void		SetAnimation(int num_frames, float speed){m_num_frames=num_frames; m_speed=1.0f/speed;}
	void		SetWidth(float width_);
	void		SetHeight(float height_);

	LPDIRECT3DTEXTURE9	GetTexture() const { return quad.tex; }
	void		GetTextureRect(float *x, float *y, float *w, float *h) const { *x=tx; *y=ty; *w=width; *h=height; }
	void		GetHotSpot(float *x, float *y) const { *x=hot_x; *y=hot_y; }
	Rect*		GetBoundingBox(float x, float y, Rect *rect) const { rect->Set(x-hot_x, y-hot_y, x-hot_x+width, y-hot_y+height); return rect; }
	float		GetWidth() const { return width; }
	float		GetFrameHeight() const { return frame_height; }
	float		GetFrameWidth() const { return frame_width; }
	float		GetHeight() const { return height; }
	UINT		GetPoolId()	const			{return m_pool_id;}
	bool		IsPlaying() const { return m_is_playing; }
	DWORD		GetColor(int i=0) const { return quad.v[i].color; }
	int			GetBlendMode() const { return quad.blend; }

	void		Release();
};





/****************************************************************************************
		
		Resource script example:

*****************************************************************************************

[sprite]
base		sprite0
name		sprite_name
texture		texname.tga
rect		0  0  64  64
position	200  100
hotspot		10 10
frames		3
speed		10
animation	FORWARD REVERSE PINGPONG LOOP
play		1
color		255 255 0 255
blend		COLORADD COLORMUL ALPHAADD

*****************************************************************************************/